/*   
 * Copyright (c) 2012 by XUANWU INFORMATION TECHNOLOGY CO. 
 *             All rights reserved                         
 */
package com.system.web.common.utils;

import com.system.web.common.entity.Permission;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * @Description: 父子权限归属处理和排序
 * @Author <a href="jiji@javawind.com">Xuefang.Xu</a>
 * @Date 2012-9-7
 * @Version 1.0.0
 */
public class PermissionUtil {

    private final static String CHILD = "：";
    private final static String COM = "，";
    private final static String SEM = "；";
    private final static String LP = "(";
    private final static String RP = ")";

    /**
     * 排列和归类权限
     *
     * @param permissions
     * @return
     */
    public static List<Permission> sortPermissions(List<Permission> permissions) {
        if (ListUtil.isBlank(permissions)) {
            return Collections.emptyList();
        }

        Map<Integer, Permission> map = new HashMap<Integer, Permission>();
        for (Permission p : permissions) {
            map.put(p.getId(), p);
            p.setChildren(null);// 重置
        }

        for (Entry<Integer, Permission> entry : map.entrySet()) {
            Permission p = entry.getValue();
            int parentId = p.getParentId();
            if (parentId > 0) {
                Permission parent = map.get(parentId);
                if (parent != null) {
                    parent.addToChildren(p);
                }
            }
        }

        List<Permission> list = new ArrayList<Permission>();
        for (Entry<Integer, Permission> entry : map.entrySet()) {
            if (entry.getValue().getParentId() == 0) {
                list.add(entry.getValue());
            }
        }

        doSortPermissions(list);
        for (Permission perm : list) {
            List<Permission> children = perm.getChildren();
            doSortPermissions(children);
        }
        return list;
    }

    public static void doSortPermissions(List<Permission> list) {
        if (ListUtil.isBlank(list)) {
            return;
        }
        Collections.sort(list, new Comparator<Permission>() {
            @Override
            public int compare(Permission o1, Permission o2) {
                return o1.getId() - o2.getId();
            }
        });
    }


    /**
     * 选中权限
     *
     * @param permissions
     * @param checked
     * @return
     */
    public static List<Permission> checkPermissions(
            List<Permission> permissions, List<Permission> checked) {
        if (checked == null || checked.size() == 0) {
            return permissions;
        }

        for (Permission p : permissions) {
            for (Permission c : checked) {
                if (c.getId() == p.getId()) {
                    p.setChecked(true);
                    p.setDataScopeId(c.getDataScopeId());
                    break;
                }
            }
            List<Permission> children = p.getChildren();
            if (children != null && children.size() > 0) {
                checkPermissions(children, checked);
            }
        }

        return permissions;
    }

    /**
     * 生成权限字符串
     *
     * @param permissions
     * @return
     */
    public static String getPermissionsStr(List<Permission> permissions) {
        if (permissions == null || permissions.size() == 0) {
            return "";
        }

        List<Permission> list = sortPermissions(permissions);

        StringBuffer sb = new StringBuffer();
        for (Permission p : list) {
            sb.append(getPermissionStr(p) + SEM);
        }
        return sb.toString();
    }

    /**
     * 生成权限字符串
     *
     * @param permission
     * @return
     */
    public static String getPermissionStr(Permission permission) {
        if (permission == null) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        sb.append(permission.getName());
        if (permission.getParentId() == 0) {
            sb.append(CHILD);
        }

        if (permission.getChildren() != null) {
            if (permission.getParentId() > 0) {
                sb.append(LP);
            }
            for (Permission c : permission.getChildren()) {
                String str = sb.toString();
                if (!str.endsWith(CHILD) && !str.endsWith(LP)) {
                    sb.append(COM);
                }
                sb.append(getPermissionStr(c));
            }
            if (permission.getParentId() > 0) {
                sb.append(RP);
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        Map<Integer, String> map = new TreeMap<Integer, String>();
        map.put(1, "1");
        map.put(1, "2");
        System.out.println(map.size());
    }
}
